package com.longlonggo.action;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.longlonggo.action.BaseAction;

/**
 * 错误页面控制层
 * 
 * @author me@longlonggo.com
 * @version 100-000-000<br/>
 *          创建日期：2018年7月17日<br/>
 *          历史修订：<br/>
 */
@Controller
public class ErrorAction extends BaseAction implements ErrorController {

	@RequestMapping("/error")
	public String handleError(HttpServletRequest request) {
		Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
		logger.error("-----发生错误"+status);
		if (status != null) {
			Integer statusCode = Integer.valueOf(status.toString());
			if (statusCode == HttpStatus.NOT_FOUND.value()) {
				return "/error/404";
			} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
				return "/error/500";
			}
		}
		return "/error/500";
	}

	@Override
	public String getErrorPath() {
		return "/error";
	}
}
